package cn.fl.ass.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssParkingService;
import cn.fl.ass.transform.AssParkingMgVOTransform;
import cn.fl.ass.transform.AssParkingVOTransform;
import cn.fl.ass.vo.AssParkingMgVO;
import cn.fl.ass.vo.AssParkingVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import fl.ass.facade.api.IAssParkingApi;
import fl.ass.facade.condition.AssParkingMgQueryCondition;
import fl.ass.facade.condition.AssParkingQueryCondition;
import fl.ass.facade.dto.AssParkingDTO;
import fl.ass.facade.dto.AssParkingMgDTO;
import fl.ass.facade.dto.MessageDTO;

/**
 * @Description: 停车场表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssParkingServiceImpl implements IAssParkingService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IAssParkingService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IAssParkingApi assParkingApi;

	@Override
	public AssParkingVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		//查询停车场信息
		AssParkingDTO dto = this.assParkingApi.selectAssParkingById(id);
		return AssParkingVOTransform.toVO(dto);
	}

	@Override
	public Long add(AssParkingVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		//返回新增ID值-JL-20170308
		MessageDTO<String> response = this.assParkingApi.saveAssParking(AssParkingVOTransform.toDTO(entity));
		return response.getId();
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//return this.assParkingApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AssParkingVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		//返回逻辑值-JL-20170315
		MessageDTO<String> response = this.assParkingApi.updateAssParking(AssParkingVOTransform.toDTO(entity));
		return response.isSuccess();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.assParkingApi.findAssParkingsByPager(page);
		List<AssParkingDTO> list = (List<AssParkingDTO>)page.getResultList();
		page.setResultList(AssParkingVOTransform.toVOList(list));
		return page;
	}
	
	/**
	 * 导出停车场信息
	 */
	@Override
	public String exportAssParking(AssParkingQueryCondition condition, String orgCd) throws BaseException {
		if(null==condition) condition = new AssParkingQueryCondition();
		if(StringUtils.isEmpty(orgCd))orgCd = CurrentThreadContext.getCurrentOrgCd();
		return this.assParkingApi.exportAssParking(condition, orgCd);
	}
	
	/**
	 * 根据城市代码获取停车场名称数据
	 * @param regCd
	 * @return
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> searchParkNmKVList(String provinceCd, String cityCd, String parkId) throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		List<AssParkingVO> volist = new ArrayList<AssParkingVO>();
		
		if(StringUtils.isEmpty(provinceCd) || StringUtils.isEmpty(cityCd)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		List<AssParkingDTO> dtolist = new ArrayList<AssParkingDTO>();
		dtolist = this.assParkingApi.getParkNmByRgnCd(provinceCd, cityCd, parkId);
		volist = AssParkingVOTransform.toVOList(dtolist);
		
		if(CollectionUtils.isNotEmpty(volist)){
			for (AssParkingVO vo : volist) {
				KeyValueVO kv = new KeyValueVO();
				kv.setKey(vo.getId().toString());//停车场ID
				kv.setValue(vo.getParkNm());//停车场名称
				kvList.add(kv);
			}
		}else{
		    BaseLogUtils.newLogger("根据该城市代码获取停车场名称数据为空").info();
		}
		return kvList;
	}
	
	/**
	 * 根据停车场ID获取资产管理员数据
	 * @param parkId
	 * @return
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> searchAssMgsKVList(String parkId) throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		List<AssParkingMgVO> volist = new ArrayList<AssParkingMgVO>();
		//判断参数是否为空
		if(StringUtils.isEmpty(parkId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}else{
			Long id = Long.parseLong(parkId);
			List<AssParkingMgDTO> dtolist = new ArrayList<AssParkingMgDTO>();
			//设置查询条件
			AssParkingMgQueryCondition qc = new AssParkingMgQueryCondition();
			qc.setParkId(id);
			qc.setIsDel(BaseConstant.IS_YESNO_NO);
			dtolist = this.assParkingApi.searchAssMgsList(qc);
			volist = AssParkingMgVOTransform.toVOList(dtolist);
			
			if(CollectionUtils.isNotEmpty(volist)){
				for (AssParkingMgVO vo : volist) {
					if (vo.getManagerId() != null) {
						KeyValueVO kv = new KeyValueVO();
						kv.setKey(vo.getManagerId().toString());//资产管理员ID
						kv.setValue(vo.getManagerRlNm()+"["+vo.getManagerNm()+"]");//资产管理员名称
						kvList.add(kv);
					}
				
				}
			}else{
			    BaseLogUtils.newLogger("根据该城市代码获取停车场名称数据为空").info();
			}
		}
		return kvList;
	}

	@Override
	public List<AssParkingMgVO> selectManagerList() throws BaseException {
		List<AssParkingMgDTO> dtoList = this.assParkingApi.selectManagerList();
		List<AssParkingMgVO> voList = new ArrayList<AssParkingMgVO>();
		voList = AssParkingMgVOTransform.toVOList(dtoList);
		return voList;
	}
}
